/************************************************************
 * Copyright (c) 2010 Georg Fritzsche
 *
 * This project is licensed under the "New BSD License": 
 * http://www.opensource.org/licenses/bsd-license.php
 ***********************************************************/

#include <fmod.hpp>
#include <fmod_errors.h>

#include "track.h"
#include "audio_system.h"
#include "fmod_util.h"
#include "util.h"


Track::Track(const std::string& path, const boost::shared_ptr<AudioSystem>& system, bool stream)
  : m_path(path)
  , m_title()
  , m_sound(system->createSound(path, stream, AudioSystem::AudioSystemKey()))
  , m_channel()
  , m_system(system)
{
    FMOD_TAG fmTag;
    FMOD_RESULT fmRes;

    fmRes = m_sound->getTag("TITLE", 0, &fmTag);

    if(isFmodErr(fmRes)) {
        m_title = "[" + m_path + "]";
    } else {
        m_title = (char*)fmTag.data;
    }
};

Track::~Track()
{
    
};

void Track::play()
{
    m_channel = m_system->playSound(m_sound, AudioSystem::AudioSystemKey());
};

void Track::stop() const
{
    FMOD_RESULT fmRes;

    if (m_channel.get()) {
        fmRes = m_channel->stop();
    }
};

const std::string& Track::title() const
{
    return m_title;
};
